﻿using System;
using System.Data.Linq;
using System.Linq;

namespace eCommerce.DataAccess
{
    public class OrderDA
    {
        public static string GetCardName(int cardTypeId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var cardName = (eCommerceDataContext.CardTypes.Where(ct => ct.ID == cardTypeId).Select(ct => ct.Name)).FirstOrDefault();

            return cardName;
        }
        public static string GetOrderRefNum(Guid cartId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var orderRef = (from or in eCommerceDataContext.Orders
                            where or.cartid == cartId
                            select or.orderref).FirstOrDefault();
            return Convert.ToString(orderRef);
        }

        public static string GetCardValue(int cardtypeId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var cardTypeId = (eCommerceDataContext.CardTypes.Where(ct => ct.ID == cardtypeId).Select(ct => ct.Type)).FirstOrDefault();

            return cardTypeId;
        }
        public static int CardTypeId(string cardType)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var CardType = (eCommerceDataContext.CardTypes.Where(ct => ct.Type == cardType).Select(ct => ct.ID)).FirstOrDefault();
            return CardType;
        }

        public static Address GetDeliveryAddress(Guid deliveryaddressId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return (eCommerceDataContext.Addresses.Where(b => b.AddressID == deliveryaddressId)).FirstOrDefault();
        }

        public static Country_Code GetCountryCodes(int countryCodeId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return (eCommerceDataContext.Country_Codes.Where(cc => cc.ID == countryCodeId)).FirstOrDefault();
        }

        public static Address GetBillingAddress(Guid billingAddressId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return (eCommerceDataContext.Addresses.Where(b => b.AddressID == billingAddressId)).FirstOrDefault();

        }
       
        public static CustomerCard GetCardDetails(Guid userId)
        {
            PaymentDataContext paymentDataContext = new PaymentDataContext();
            return (paymentDataContext.CustomerCards.Where(cd => cd.userID == userId)).FirstOrDefault();
        }

        public static IQueryable<decimal?> GetCartPrice(Guid cartId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return (eCommerceDataContext.Cart_Products.Where(p => p.cartid == cartId).Select(p => p.price));
        }

        public static ISingleResult<getCardTypeResult> GetCardType()
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return eCommerceDataContext.getCardType();
        }

        public static void InsertCustomerPaymentDetails(CustomerCard customerCard)
        {
            PaymentDataContext paymentDataContext = new PaymentDataContext();

            paymentDataContext.CustomerCards.InsertOnSubmit(customerCard);
            paymentDataContext.SubmitChanges();
        }

        public static void UpdateCustomerPaymentDetails(CustomerCard card)
        {
            PaymentDataContext paymentDataContext = new PaymentDataContext();
            paymentDataContext.SubmitChanges();
        }

        public static void InsertOrderDetails(Order order)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.Orders.InsertOnSubmit(order);
            eCommerceDataContext.SubmitChanges();
        }

        public static void UpdateOrderDetails(Order order)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.SubmitChanges();
        }

        public static void InsertOrderProductDetails(OrderDetail orderdetail)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.OrderDetails.InsertOnSubmit(orderdetail);
            eCommerceDataContext.SubmitChanges();
        }

        public static void UpdateOrderProductDetails(OrderDetail orderdetail)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.SubmitChanges();
        }


        public static void DeleteOrderDetails(Order order)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.Orders.DeleteOnSubmit(order);
            eCommerceDataContext.SubmitChanges();
        }

        public static Order GetOrderDetails(Guid userId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return (eCommerceDataContext.Orders.Where(or => or.customerid == userId)).FirstOrDefault();
        }

        public static void UpdateOrderStatus(Guid cartId, int newStatus)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var oStatus = (eCommerceDataContext.Orders.Where(o => o.cartid == cartId).Select(o => o.orderstatus)).FirstOrDefault();
            var oldStatus = (from os in eCommerceDataContext.Orders
                             where os.cartid == cartId && os.orderstatus == oStatus
                             select os).FirstOrDefault();
            oldStatus.orderstatus = newStatus;
            eCommerceDataContext.SubmitChanges();
        }
        public static void UpdateOrderProductsStatus(Guid orderId, int newStatus)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var oStatus = (eCommerceDataContext.OrderDetails.Where(o => o.orderid == orderId));
            foreach (var product in oStatus)
            {
                product.status = newStatus;
            }
            eCommerceDataContext.SubmitChanges();
        }
        
        public static Order CheckRandonNumber(int randomNumber)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            return (eCommerceDataContext.Orders.Where(or => or.orderref == randomNumber)).FirstOrDefault();
        }
        public static void InsertCartProduct(Cart_Product cartProduct)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.Cart_Products.InsertOnSubmit(cartProduct);
            eCommerceDataContext.SubmitChanges();
        }

        public static void UpdateCartProduct(Cart_Product cartProduct)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            eCommerceDataContext.SubmitChanges();
        }

         public static decimal GetProductPrice(Int32 itemId)
         {
             SiteProductsDataContext siteProductsDataContext = new SiteProductsDataContext();
             var price = (from pr in siteProductsDataContext.Products
                         where pr.ProductID == itemId
                         select pr).FirstOrDefault();
             return (decimal) price.UnitPrice;
             
         }
        public static int GetProductType(Int32 itemId)
        {
            SiteProductsDataContext siteProductsDataContext = new SiteProductsDataContext();
            var type = (from tp in siteProductsDataContext.Products
                        where tp.ProductID == itemId
                        select tp).FirstOrDefault();
            return type.ProductTypeID;
            
        }

        public static bool CheckProductCart(Guid cartId, int itemId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var checkCart = (eCommerceDataContext.Cart_Products.Where(pr => pr.cartid == cartId && pr.qty > 0)).FirstOrDefault();
                    return checkCart != null && checkCart.productid == itemId;
        }

        public static Cart_Product GetProductCart(Guid cartId, int itemId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var productCart = (from pc in eCommerceDataContext.Cart_Products
                              where pc.cartid == cartId && pc.productid == itemId && pc.qty > 0
                              select pc).FirstOrDefault();
            return productCart;
        }
        
        public static int GetProductCartQty(Guid cartId, int itemId)
        {
            eCommerceDataContext eCommerceDataContext = new eCommerceDataContext();
            var productCart = (from pc in eCommerceDataContext.Cart_Products
                              where pc.cartid == cartId && pc.productid == itemId && pc.qty > 0
                              select pc).FirstOrDefault();
            return productCart.qty;
        }
        public static Product GetProduct(int itemId)
        {
            SiteProductsDataContext siteProductsDataContext = new SiteProductsDataContext();
            var product = (from pd in siteProductsDataContext.Products
                           where pd.ProductID == itemId
                           select pd).FirstOrDefault();
            return product;
        }


        public static ISingleResult<getProductbyCategoryResult> GetProductByCategory(int _item)
        {
            SiteProductsDataContext siteProductsDataContext = new SiteProductsDataContext();
            return siteProductsDataContext.getProductbyCategory(_item);
        }
    }

    
}
